using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SmartLink.DTOS.SysBasic;
using SmartLink.Domain.Entity;
using SmartLink.Infrastructure;
using SmartLink.Infrastructure.Cache;
using SmartLink.IRepository;
using SmartLink.Repository;
using SmartLink.Domain.IServices;
using SmartLink.EnumLibrary;
using SmartLink.Domain.ExpandEntity.Base;
using SmartLink.Domain.ExpandEntity;

namespace SmartLink.Domain.Services
{
    public class BIZ_OrganizationDepartmentServices : IBIZ_OrganizationDepartmentServices
    {
        private IBIZ_OrganizationDeptRepo _IBIZ_OrganizationDeptRepo;
        private IBIZ_OrganizationRepo _IBIZ_OrganizationRepo;
        private IBIZ_UserToOrganizationRepo _IBIZ_UserToOrganizationRepo;
        public BIZ_OrganizationDepartmentServices(IBIZ_OrganizationDeptRepo iBIZ_OrganizationDeptRepo)
        {
            _IBIZ_OrganizationDeptRepo = iBIZ_OrganizationDeptRepo;
            _IBIZ_OrganizationRepo = new BIZ_OrganizationRepo();
            _IBIZ_UserToOrganizationRepo = new BIZ_UserToOrganizationRepo();
        }

        public IList<DepartmentExpand> GetOrganizationDeptList(int oid, string keyValue, Pagination pagination)
        {
            return _IBIZ_OrganizationDeptRepo.GetOrganizationDeptList(oid, keyValue, pagination);
        }




        public void AddOrganizationDept(BIZ_OrganizationDepartment department, int curUserId, int curUserOid)
        {
            int oid = department.OID == 0 ? curUserOid : department.OID;
            department.OID = oid;

            if (department.ParentID <= 0) department.ParentID = null;

            BIZ_OrganizationDepartment maxSortDept = _IBIZ_OrganizationDeptRepo.GetMaxSortDeptByParentId(department.ParentID);
            if (maxSortDept == null)
                department.Sort = 1;
            else
                department.Sort = maxSortDept.Sort + 1;

            department.Creator = curUserId;
            department.CreateTime = DateTime.Now;
            department.Updater = curUserId;
            department.UpdateTime = DateTime.Now;
            department.IsDel = false;
            _IBIZ_OrganizationDeptRepo.Add(department, null);
        }

        public bool DeleteOrganizationDept(int orgDeptId, out string msg)
        {
            msg = "";

            if (_IBIZ_OrganizationDeptRepo.ExistDescendantDepts(orgDeptId))
            {
                msg = "该部门下存在子部门，不可删除！";
                return false;
            }

            bool existUserUnderDeptFlag = _IBIZ_UserToOrganizationRepo.CheckExistUserUnderDept(orgDeptId);
            if (existUserUnderDeptFlag)
            {
                msg = "该部门下存在用户，不可删除！";
                return false;
            }
            return _IBIZ_OrganizationDeptRepo.Delete(orgDeptId);
        }

        public IList<int> GetAllChildDeptIds(int orgDeptId)
        {
            return _IBIZ_OrganizationDeptRepo.GetSelfAndDescendantsIdList(orgDeptId);
        }


        public IList<int> FilterOID(int userId,int? oid=null)
        {
            List<int> aLLOIDS = new List<int>();
            CurrentUserInfo currentInfo = UserInfoCaches.GetUserInfoCache(userId);
            if (oid.HasValue)
            {
                aLLOIDS.Add(oid.Value);
                return aLLOIDS;
            }

            //公司管理员
            if (ConstData.OrgManagerRoleName == currentInfo.RoleName)
            {
                aLLOIDS.Add(currentInfo.OID);
            }
            //超级管理员
            else if (ConstData.SuperManagerRoleName == currentInfo.RoleName)
            {
                //不需要添加过滤
                aLLOIDS = new List<int>();
            }
            else //普通用户
            {
                aLLOIDS.Add(currentInfo.OID);
            }

            return aLLOIDS;
        }


        /// <summary>
        /// 超管返回null 其他返回当前用户的oid
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public int? GetOID(int userId)
        {
            int? oid = null;
            CurrentUserInfo currentInfo = UserInfoCaches.GetUserInfoCache(userId);
            if (ConstData.SuperManagerRoleName != currentInfo.RoleName)
            {
                oid = currentInfo.OID;
            }
            return oid;
        }
        public IList<BIZ_OrganizationDepartment> GetAllChildDepts(IList<int> deptIds)
        {
            return _IBIZ_OrganizationDeptRepo.GetAllChildDepts(deptIds);
        }

        public bool UpdateOrganizationDept(BIZ_OrganizationDepartment bIZ_OrganizationDepartment)
        {
            return _IBIZ_OrganizationDeptRepo.Update(bIZ_OrganizationDepartment, null);
        }

        public BIZ_OrganizationDepartment GetSingleOrganizationDept(int orgDeptId)
        {
            return _IBIZ_OrganizationDeptRepo.GetSingleOrganizationDepartment(orgDeptId);
        }

        public bool CheckExistByDeptName(int oid, string DeptName)
        {
            return _IBIZ_OrganizationDeptRepo.CheckExistByDeptName(oid, DeptName);
        }

        public IList<BIZ_OrganizationDepartment> GetAllDeptByOrgId(int orgId)
        {
            return _IBIZ_OrganizationDeptRepo.GetOrgDeptList(orgId).ToList();
        }

        public IEnumerable<BIZ_OrganizationDepartment> GetDeptDescendants(int topOdid, bool withSelf = false)
        {
            return _IBIZ_OrganizationDeptRepo.GetDescendants(topOdid, withSelf);
        }
        /// <summary>
        /// 获取所有门店
        /// </summary>
        /// <param name="orgId"></param>
        /// <returns></returns>
        public IList<BIZ_OrganizationDepartment> GetAllStoreByOrgId(int orgId)
        {
            return _IBIZ_OrganizationDeptRepo.GetAllStoreByOrgId(orgId);
        }
    }
}
